<?php
echo $this->headScript()->setFile($this->baseUrl().'/js/jquery/table-sorter/jquery.dataTables.js');
?>
<div id="container">
<?php if (!$this->allow()) : ?>
	<div class="grid_12 t_a_bottom">
		<?php echo $this->translator->translator('Member_Access_deny_desc'); ?>
	</div>
<?php else : ?>
<script type="text/javascript">
$(document).ready(function() {
	floatingbar('#floatingbar',"#button_top_bar",null);
	/****************************Sorting Table Start*********************/
	var oTable = $("#sortTable").dataTable({
				"bPaginate": false,
				"bLengthChange": true,
				"bSearchable": true,
				"bFilter": true,
				"bSort": true,
				"bInfo": false,
				"bAutoWidth": true,
				"bProcessing": false,
				"bSortClasses": true,
				"bStateSave": false,
				"bServerSide": false,
				"aaSorting": [[ 1, "asc" ]],				
				"aoColumnDefs": [ 
						{ "bSortable": false, "bSortClasses": false,  "aTargets": [ 0 ] }
					],
				"oLanguage" : { "sSearch": "<?php echo $this->translator->translator('jquery_filter'); ?>" }
			}); 	
	
	$('td', oTable.fnGetNodes()).hover( function() {		
		$(this).parents("tr:first").children('td').addClass( 'highlighted' );
	}, function() {
		$('td.highlighted').removeClass('highlighted');
	});
	
	/****************************Sorting Table End*********************/
	
	
	$('a.delete_all').click(function() {
		var self = this;		
		$("#dialog-confirm").html("<?php echo addslashes($this->translator->translator('utility_cache_refresh_confirm')); ?>").dialog({
				resizable: false,
				width:400,
				height:170,
				modal: true,
				show: 'fade',
				hide: 'explode',
				buttons: {
						'<?php echo html_entity_decode($this->translator->translator("common_refresh_selected"), ENT_QUOTES, "UTF-8"); ?>': function() {
						refreshAll(self);
						$(this).dialog('close');
					},
				'<?php echo html_entity_decode($this->translator->translator("common_cancel"), ENT_QUOTES, "UTF-8"); ?>': function() {
						$(this).dialog('close');
					}
				}
			});			
	});	
	
	$('input.checkAll_btn').click(function() {
		var self = this;
		if($(self).is(':checked'))
		{		
			$('input.check_btn').attr('checked', true);
		}
		else
		{
			$('input.check_btn').attr('checked', false);
		}
	});	
	
	allFunction();
	
});



/* Get the rows which are currently selected */
function fnGetSelected( oTableLocal )
{
	var aReturn = new Array();
	var aTrs = oTableLocal.fnGetNodes();
	
	for ( var i=0 ; i<aTrs.length ; i++ )
	{
		if ( $(aTrs[i]).hasClass('row_selected') )
		{
			aReturn.push( aTrs[i] );
		}
	}
	return aReturn;
}

function openMsgDialog(msg)
{
	$('#dialog_msg').html(msg).dialog({
		autoOpen: true,
		modal: false,
		show: 'fade',
		resizable: 'false',				
		buttons: { "<?php echo html_entity_decode($this->translator->translator('common_ok'), ENT_QUOTES, 'UTF-8'); ?>": function() { $(this).dialog('option', 'hide', 'explode').dialog("close"); } },
		open: function(event, ui)
			 {
				setTimeout(function() 
				{									
					if($('#' + event.target.id).dialog('isOpen'))
					{
						$('#' + event.target.id).dialog('option', 'hide', 'explode').dialog('close');										
					}
				}, 10000);
			  }
	});
}

function refreshAll(self)
{	
	$.ajax({
			url: "<?php echo $this->url( array('module'=> 'Utility', 'controller' => 'backend', 'action'     => 'cache' ), 'adminrout',    true) ?>",
			type: 'POST',
			data: { id_st: 'refresh'},
			beforeSend: function(){
				$(self).html('<span class="icon loader"></span>');
			 },
			success: function(response) 
			{
				//alert(response);
				var json_arr = eval("("+response+")");
				if(json_arr.status == 'ok')
				{																								
					$('#sortTable').children('tbody:first').children('tr').addClass('row_selected');					
					
					var oTabled = $('#sortTable').dataTable();
					var anSelected = fnGetSelected( oTabled );
					for(var k = 0; k < anSelected.length; k++)
					{
						oTabled.fnDeleteRow( anSelected[k] );
					}
				}
				else
				{							
					openMsgDialog(json_arr.msg);
				}	
				$(self).html('<span class="icon reset"><?php echo $this->translator->translator("common_refresh_selected"); ?></span>');
			},
			error: function(xhr, status, error){
				var msg = "Error! " + xhr.status + " " + error;
				openMsgDialog(msg);
			}
	});	
}



function deleteAll(self)
{
	var id_str = '';
	for(var i=0;i < $('input.check_btn:checked').length;i++)
	{
		if(i == 0)
		{
			 id_str += $('input.check_btn:checked:eq('+i+')').val();
		}
		else
		{
			id_str += ','+$('input.check_btn:checked:eq('+i+')').val();
		}
	}
	
	$.ajax({
			url: "<?php echo $this->url( array('module'=> 'Database', 'controller' => 'csv', 'action'     => 'deleteall' ), 'adminrout',    true) ?>",
			type: 'POST',
			data: { id_st: id_str},
			beforeSend: function(){
				$(self).html('<span class="icon loader"></span>');
			 },
			success: function(response) 
			{
				//alert(response);
				var json_arr = eval("("+response+")");
				if(json_arr.status == 'ok')
				{
					for(var i= ($('input.check_btn:checked').length-1);i >= 0;i--)
					{								
						$('input.check_btn:checked:eq('+i+')').parents("td:first").parents("tr:first").addClass('row_selected');																			
					}
					var oTabled = $('#sortTable').dataTable();
					var anSelected = fnGetSelected( oTabled );
					for(var k = 0; k < anSelected.length; k++)
					{
						oTabled.fnDeleteRow( anSelected[k] );
					}
				}
				else
				{							
					openMsgDialog(json_arr.msg);
				}	
				$(self).html('<span class="icon reset"><?php echo $this->translator->translator("common_refresh_selected"); ?></span>');
			},
			error: function(xhr, status, error){
				var msg = "Error! " + xhr.status + " " + error;
				openMsgDialog(msg);
			}
	});	
}

function deleteThis(self)
{
	var file_name = $(self).attr('rel');
	$.ajax({
		 url: "<?php echo $this->url( array('module'=> 'Database', 'controller' => 'csv', 'action'     => 'delete' ), 'adminrout',    true) ?>",
		 type: 'POST',
		 data: { file_name: file_name },
		 beforeSend: function(){
				$(self).html('<img src="application/modules/Administrator/layouts/scripts/images/loader/ajax_loader.gif" border="0" height="16" />');
			 },
		 success: function(response) 
		 {
			//alert(response);
			var json_arr = eval("("+response+")");
			if(json_arr.status == 'ok')
			{
				$(self).parents("li:first").parents("ul:first").parents("td:first").parents("tr:first").addClass('row_selected');
				var oTables = $('#sortTable').dataTable();
				var anSelected = fnGetSelected( oTables );
				oTables.fnDeleteRow( anSelected[0] );
			}
			else
			{
				openMsgDialog(json_arr.msg);
			}
		 },
		error: function(xhr, status, error){
			var msg = "Error! " + xhr.status + " " + error;
			openMsgDialog(msg);
		}
	});
}


function allFunction()
{	
	$('a.delete_btn').unbind('click');
	$('a.delete_btn').click(function() {
		var self = this;
		$("#dialog-confirm").html("<?php echo addslashes($this->translator->translator('utility_cache_refresh_confirm')); ?>").dialog({
			resizable: false,
			width:400,
			height:170,
			modal: true,
			show: 'fade',
			hide: 'explode',
			buttons: {
					'<?php echo html_entity_decode($this->translator->translator("common_delete"), ENT_QUOTES, "UTF-8"); ?>': function() {
					deleteThis(self);
					$(this).dialog('close');
				},
			'<?php echo html_entity_decode($this->translator->translator("common_cancel"), ENT_QUOTES, "UTF-8"); ?>': function() {
					$(this).dialog('close');
				}
			}
		});		
	});
}
</script>

<div class="title-container">
	<div class="cache-refresh"><h1><?php echo $this->translator->translator('Utility_cache_page_name'); ?></h1></div>
    
    <div class="button-container">
        <?php  if ($this->allow('cache','backend','Utility')) : ?>
        <span class="button  ui-widget-content-1"><a href="javascript: void(0);" class="delete_all"><span class="icon reset"><?php echo $this->translator->translator('common_refresh_selected'); ?></span></a></span>
        <?php endif; ?>
	</div>
</div>

<div class="content-container">
	<div id="button_top_bar"></div>
    
    <div class="pagination">
		<?php echo $this->paginationControl($this->datas, 'Sliding', 'top_pagination.phtml'); ?>
    </div>
    
    <div class="chart">
   		<div class="data-table">
        	<table id="sortTable" width="100%" align="center" cellpadding="0" cellspacing="0">
                <thead>
                    <tr>
                    	<th width="88%"><?php echo $this->translator->translator('utility_cache_file_list'); ?></th>
                      	<th width="12%"><?php echo $this->translator->translator('common_date'); ?></th>
                   	</tr>
             	</thead>
                <tbody>
                <?php 
                $j = 1;
                foreach ($this->datas as $entry): 													
                    if($j % 2 == 0)
                    {
                        $onmouseout = "this.bgColor='#FFFFFF';";
                        $background = '#FFFFFF';
                    }
                    else
                    {
                        $onmouseout = "this.bgColor='#F7F7F7';";
                        $background = '#F7F7F7';
                    }																									
                ?>
              		<tr bgcolor="<?php echo $background; ?>" onmouseover="this.bgColor='#FCF5DD';" onmouseout="<?php echo $onmouseout; ?>">
                    	<td><?php echo $this->escape($entry['name']); ?></td>
                      	<td class="center"><?php echo date("m/d/Y",filemtime(BASE_PATH.DS.$this->escape($entry['path']).DS.$this->escape($entry['name']))); ?></td>
                    </tr>
                <?php 													
                    $j++;
                    endforeach;	
              	?>
               	</tbody>
			</table>
       	</div>
	</div>
            
    <div class="pagination">
		<?php echo $this->paginationControl($this->datas, 'Sliding', 'pagination.phtml'); ?>
    </div> 
</div>

<div id="dialog_msg" title="<?php echo $this->translator->translator('common_dialog_msg',$this->translator->translator('Utility_cache_page_name')); ?>"></div>
<div id="dialog-confirm" title="<?php echo $this->translator->translator('common_confirmation',$this->translator->translator('common_refresh')); ?>"></div>	
<div id="restore-confirm" title="<?php echo $this->translator->translator('utility_cache_refresh_confirm'); ?>"></div>

<div id="floatingbar">
	<h1><?php echo $this->translator->translator('Utility_cache_page_name'); ?></h1>
    
    <div class="button-container">
        <?php  if ($this->allow('cache','backend','Utility')) : ?>
        <span class="button  ui-widget-content-1"><a href="javascript: void(0);" class="delete_all"><span class="icon reset"><?php echo $this->translator->translator('common_refresh_selected'); ?></span></a></span>
        <?php endif; ?>
	</div>
</div>
<?php endif; ?>
</div>